package com.warmheart.impl.user;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.warmheart.base.config.RedisService;
import com.warmheart.base.service.impl.BaseService;
import com.warmheart.core.constant.RedisConstant;
import com.warmheart.core.enums.DELETED;
import com.warmheart.core.util.RandomUtils;
import com.warmheart.dao.sys.SysConfigDao;
import com.warmheart.dao.user.UserLoginDao;
import com.warmheart.entity.user.UserLogin;
import com.warmheart.model.VerifiedResultModel;
import com.warmheart.service.user.IUserLoginService;

/**
 * 
 * @ClassName: UserLoginService
 * @Description: 用户登录Service，对应表名称为tb_user_login
 *               温馨提示：对外提供的接口，原则上这里只写自定义方法
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
@Service(value = "userLoginService")
public class UserLoginService extends BaseService<UserLogin> implements IUserLoginService {

    Logger logger = LoggerFactory.getLogger(UserLoginService.class);

    @Autowired
    private UserLoginDao userLoginDao;

    @Autowired
    private SysConfigDao sysConfigDao;

    @Autowired
    private RedisService redisService;

    @Override
    public UserLoginDao getDao() {
        return this.userLoginDao;
    }

    @Override
    public VerifiedResultModel verifyLoginSmsCode(String loginName, String smsCode) {
        VerifiedResultModel verifyResult = new VerifiedResultModel();
        // 从缓存中获取验证码
        // 验证码校验
        Object smsCodeObj = redisService.get(RedisConstant.REGISTER_SMS_CODE + loginName);
        if (smsCodeObj == null) {
            verifyResult.setVerifyFlag(false);
            verifyResult.setVerifyMsg("验证码已过期");
            return verifyResult;
        }
        String smsCodeStr = smsCodeObj.toString();
        if (StringUtils.isBlank(smsCodeStr)) {
            verifyResult.setVerifyFlag(false);
            verifyResult.setVerifyMsg("验证码已过期");
            return verifyResult;
        }
        if (!smsCodeStr.equals(smsCode)) {
            verifyResult.setVerifyFlag(false);
            verifyResult.setVerifyMsg("验证码错误");
            return verifyResult;
        }
        try {
            // 验证通过之后，删除验证码缓存
            redisService.del(RedisConstant.REGISTER_SMS_CODE + loginName);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除短信验证码缓存失败", e);
        }
        verifyResult.setVerifyFlag(true);
        verifyResult.setVerifyMsg("验证通过");
        return verifyResult;
    }

    /**
     * 
     * @Title: register
     * @Description: 注册用户
     * @author ZhangJianyang
     * @param loginName
     * @throws Exception
     */
    @Override
    public void register(String loginName) throws Exception {
        Date now = new Date();
        // TODO 创建用户账号，获取用户ID
        // TODO 伪代码
        Integer userId = RandomUtils.getRandom(10000);
        // 创建登录信息
        UserLogin userLogin = new UserLogin();
        userLogin.setUserId(userId);
        userLogin.setLoginType("01");
        userLogin.setLoginName(loginName);
        // 设置用户登录默认可用
        userLogin.setStatus("01");
        userLogin.setCstCreate(now);
        userLogin.setCstModify(now);
        userLogin.setDelFlag(DELETED.DELETED_NO.getValue());
        this.userLoginDao.add(userLogin);
    }
}
